﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Input;

using SBPweb.RIA.Shared.Contracts.Events;
using SBPweb.RIA.Client.Controls.Content;

using SBPweb.PROvibe.Shared.Library.Components;

using SBPweb.PROvibe.Client.Library.Base;
using SBPweb.PROvibe.Client.Library.Controls;

using SBPweb.PROvibe.Client.Core.Models;

namespace SBPweb.PROvibe.Client.Core.Controls
{

    public partial class ModuleBar : ZoneUserControl
    {

        #region Constructor logic

        public ModuleBar()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        protected override void InitializeHandlers()
        {
            base.InitializeHandlers();
        }

        protected override void FinalizeHandlers()
        {
            base.FinalizeHandlers();
        }

        protected override void OnLoaded(object sender, RoutedEventArgs e)
        {
            base.OnLoaded(sender, e);
        }

        protected override void OnUnloaded(object sender, RoutedEventArgs e)
        {
            base.OnUnloaded(sender, e);
        }

        #endregion

        #region Event handlers

        //private void ToolBarItem_Loaded(object sender, RoutedEventArgs e)
        //{
        //    ToolBarItem tbi = sender as ToolBarItem;
        //    IClientModule module = tbi.DataContext as IClientModule;
        //    string asmName = module.GetType().Assembly.FullName;
        //    asmName = asmName.Substring(0, asmName.IndexOf(",")).TrimEnd();
        //    tbi.ImageSource = new BitmapImage(new Uri(string.Format("/{0};component/Resources/ModuleIcon_256x256.png", asmName), UriKind.RelativeOrAbsolute));
        //}

        private void Button_Loaded(object sender, RoutedEventArgs e)
        {
            ContentControl control = sender as ContentControl;
            IClientModule module = control.DataContext as IClientModule;
            AsyncRequestArgs<ModuleBarItemBase> args = new AsyncRequestArgs<ModuleBarItemBase>(new AsyncRequestHandler<ModuleBarItemBase>(LoadFromPackageCompleted), control);
            ClientContext.PackageLoaderController.LoadFromPackage<ModuleBarItemBase>(Application.HostBaseUri, module.Path, "Controls.ModuleBarItem", args);

            VisualStateManager.GoToState(control, "Normal", false);
        }

        private void LoadFromPackageCompleted(object sender, GenericEventArgs<ModuleBarItemBase> e)
        {
            AsyncRequestArgs<ModuleBarItemBase> args = e as AsyncRequestArgs<ModuleBarItemBase>;
            ContentControl control = args.Arguments.Get<ContentControl>(0);
            control.Content = e.Result;
            control.Visibility = Visibility.Visible;
        }

        private void Button_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(sender as Control, "MouseOver", true);
        }

        private void Button_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(sender as Control, "Normal", true);
        }

        #endregion
        
    }

}